package com.zaker.touchtv.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;

public class IOUtils {

  public static String getDirectory(String relativePath, Context context) {

    StringBuffer sb = new StringBuffer();

    if (relativePath.startsWith(File.separator)) {

      sb.append(getRootEnableDir(context).toString()).append(relativePath);
    } else {

      sb.append(getRootEnableDir(context).toString()).append(File.separator).append(relativePath);
    }

    return sb.toString();
  }

  /**
   * 获取一个可用根目录,SDcard优先
   *
   * @return 可用根目录, 若存在SDcard, 则直接返回SDcard根目录;若不存在SDcard,则返回包根目录
   */
  public static File getRootEnableDir(Context context) {

//    if (isExitStorageDevice()) {
//
//      //return Environment.getExternalStorageDirectory();
//      return context.getExternalFilesDir(null);
//    } else {

      return context.getFilesDir();
//    }
  }

  /**
   * 存在合法(支持读写)的存储设备
   */
  public static boolean isExitStorageDevice() {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
      return true;
    } else {
      return false;
    }
  }
}

